﻿using BluetoothMachine.ExpandHelper;
using BluetoothMachine.Lucifer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BluetoothMachine.Protocols
{
    /// <summary>
    /// RTU通信
    /// </summary>
    public class ModbusRTU : IProtocol
    {
        private byte _addr = 1;
        private byte _ionum;
        private bool _oddEven = false;
        /// <summary>
        /// RTU通信
        /// </summary>
        /// <param name="bytes">0-15</param>
        /// <param name="oddEven">true开 false关</param>
        public ModbusRTU(byte bytes, bool oddEven)
        {
            _oddEven = oddEven;
            _ionum = bytes;
        }
        public byte[] GetStream()
        {
            byte[] src = new byte[8];
            src[0] = _addr;
            src[1] = 0x05;
            src[2] = 0x00;
            src[3] = _ionum;
            src[4] = (byte)((_oddEven) ? 0xff : 0x00);//0xff----开  0x00----关
            src[5] = 0x00;
            ushort crc = CMBRTU.CalculateCrc(src, 6);
            src[6] = (byte)(crc & 0xff);
            src[7] = (byte)(crc >> 8);
            return src;
        }
    }
}
